package com.white.commonlib.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by ytf on 2016/08/09.
 * Description:
 * Modify by xx on 2016/08/09.
 * Modify detail:
 */
public abstract class BaseFragment extends Fragment
{

    public Context mContext;
    public Activity mActivity;

    private boolean isSetUserVisibleHintCalled;
    private boolean isUserVisiable;
    private boolean isViewCreated;
    private boolean isViewDestroyed;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser)
    {
        super.setUserVisibleHint(isVisibleToUser);
        isSetUserVisibleHintCalled = true;

        isUserVisiable = isVisibleToUser;
        if (isViewCreated && !isViewDestroyed)
        {
            isVisiable(isUserVisiable);
        }
    }

    public void isVisiable(boolean isVisiable)
    {
        if (isVisiable)
        {
            if (!isLoaded)
            {
                isLoaded = true;
                lazyLoad();
            }
        }
    }

    boolean isLoaded;

    /**
     * 懒加载
     */
    public void lazyLoad()
    {
    }

    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);
        this.mContext = context;
        mActivity = (Activity) mContext;
    }

    private boolean injected = false;
    private Unbinder binder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        injected = true;
        @LayoutRes int layout = getContentView();
        if (layout == 0)
            throw new RuntimeException("布局文件未提供");
        View v = inflater.inflate(layout, null);
        binder = ButterKnife.bind(this, v);
        return v;
    }

    public abstract @LayoutRes
    int getContentView();

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        if (!injected)
        {
            ButterKnife.bind(this, view);
        }
        isViewCreated = true;
        if (isUserVisiable)
        {
            isVisiable(true);
        }else
        {
            //发生在fragment被直接替换在布局里面的情况下
            if(!isSetUserVisibleHintCalled && !isViewDestroyed)
            {
                isVisiable(true);
            }
        }
    }


    @Override
    public void onDestroy()
    {
        if (binder != null)
        {
            binder.unbind();
        }
        super.onDestroy();
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        isViewDestroyed = true;
    }

    @Override
    public void onDetach()
    {
        super.onDetach();
        isViewDestroyed = false;
        isViewCreated = false;
    }
}
